/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans;

import Utilities.Constants;
import Utilities.Functions;
import Utilities.Interfaces.SelfValidating;
import java.io.Serializable;

/**
 * This class is used for storing internal network data when writing it to or
 * retrieving it from the database.
 *
 * @author Angel Sanadinov
 */
public class InternalNetworkBean implements Serializable, SelfValidating
{
    private String networkId;         //the UUID of the network
    private int serverId;             //the id of the server on which the network resides
    private String networkName;       //the external name of the network
    private boolean dhcpAvailable;    //true -> a dhcp server is available; false -> not avail.
    private int numberOfAttachedNICs; //the number of NICs attached to the network

    /**
     * Constructs an object using the supplied data. <br><br>
     *
     * Used when writing data to the database (DHCP data is not available).
     *
     * @param networkId the id of the network
     * @param serverId the id of the server on which the network resides
     * @param networkName the external name of the network
     */
    public InternalNetworkBean(String networkId, int serverId, String networkName)
    {
        this.networkId = networkId;
        this.serverId = serverId;
        this.networkName = networkName;
        this.dhcpAvailable = false;
        this.numberOfAttachedNICs = Constants.INVALID_NIC_NUMBER;
    }

    /**
     * Constructs an object using the supplied data. <br><br>
     *
     * Used when reading data from the database.
     *
     * @param networkId the id of the network
     * @param serverId the id of the server on which the network resides
     * @param networkName the external name of the network
     * @param dhcpAvailable DHCP server availability for the internal network
     * @param numberOfAttachedNICs the number of NICs attached to the internal network
     */
    public InternalNetworkBean(String networkId, int serverId, String networkName, 
                               boolean dhcpAvailable, int numberOfAttachedNICs)
    {
        this(networkId, serverId, networkName);
        this.dhcpAvailable = dhcpAvailable;
        this.numberOfAttachedNICs = numberOfAttachedNICs;
    }

    /**
     * Retrieves the id of the network.
     *
     * @return the network id
     */
    public String getNetworkId()
    {
        return networkId;
    }

    /**
     * Retrieves the id of the server on which the network resides.
     *
     * @return the server id
     */
    public int getServerId()
    {
        return serverId;
    }

    /**
     * Retrieves the external name of the network.
     *
     * @return the external network name
     */
    public String getNetworkName()
    {
        return networkName;
    }

    /**
     * Retrieves the DHCP server availability for the internal network.
     *
     * @return <code>true</code> if a DHCP server is available or <code>false</code>
     *         if not
     */
    public boolean isDHCPAvailable()
    {
        return dhcpAvailable;
    }

    /**
     * Retrieves the number of machine NICs attached to the internal network.
     *
     * @return the attached NICs number
     */
    public int getNumberOfAttachedNICs()
    {
        return numberOfAttachedNICs;
    }

    /**
     * Checks the validity of the object. <br><br>
     *
     * The object is considered valid only when all of its data is valid.
     *
     * @return <code>true</code> if the object is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        if(Functions.isUUIDValid(networkId) && Functions.isServerIdValid(serverId)
                && networkName != null && numberOfAttachedNICs > Constants.INVALID_NIC_NUMBER)
            return true;
        else
            return false;
    }

    /**
     * Builds a CSV list of the data stored in the object. <br><br>
     *
     * Format: <i>networkId,serverId,networkName,dhcpAvailable,numberOfAttachedNICs</i>
     *
     * @return the <code>String</code> representation of the object
     */
    @Override
    public String toString()
    {
        return networkId + "," + serverId + "," + networkName + "," + dhcpAvailable + "," + numberOfAttachedNICs;
    }
}
